//package SecureChat;
import java.io.*;
import java.net.*;
import SecureChat.ChatLib;
import java.util.*;
import SecureChat.Utilities;
import java.math.*;
import SecureChat.DHAuth;

public class SCServer {
    public static void main(String[] args)
    {
	String password;	    
	Scanner sysin;
	boolean DEBUG;
	int port = 1203;
	String serverID = "";
	String clientID = "";
	ServerSocket serv;
	Socket s = null;
	BigInteger G = null;
	byte [] readBuffer;
	ChatLib c;

	byte[] key = {0x00, 0x00, 0x00, 0x00,
		      0x00, 0x00, 0x00, 0x00,
		      0x00, 0x00, 0x00, 0x00,
		      0x00, 0x00, 0x00, 0x00};
	InputStream sIn;
	OutputStream sOut;
	//handle args
	if(args.length < 4){
	    System.out.println("This is the Server half of the secure chat program.");
	    System.out.println("usage: java SCServer -p port -s serverID [-d]");
	    System.out.println("  options");
	    System.out.println("\t-p\tPort the server is listening on");
	    System.out.println("\t-s\tID to identify this server");
	    System.out.println("\t-d\tEnable debugging mode");
	    return;
	}

	for(int i = 0; i < args.length; ++i){
	    if(args[i].charAt(0) == '-'){
		
		switch (args[i].charAt(1)){
		case 'p':
		    if(i+1 <args.length && args[i+1].charAt(0) != '-'){
			port = Integer.valueOf(args[i+1]);
			++i;
			break;
		    }

		case 's':
		    if(i+1 <args.length && args[i+1].charAt(0) != '-'){
			serverID = args[i+1];
			++i;
			break;
		    }
		case 'd':
		    //Load debugging
		    Properties p = new Properties(System.getProperties());
		    p.setProperty("DEBUG", "true");
		    System.setProperties(p);
		    break;
		default:
		    System.out.println("Unrecognized or incomplete arguement");
		    System.exit(1);
		    break;
		}
	    }
	}
	//end handle args

	//Check debug mode
	if(System.getProperty("DEBUG") != null){
	    DEBUG = true;
	    System.err.println("Debugging mode enabled.");
	}
	else{
	    DEBUG = false;
	}

	try{
	    serv = new ServerSocket(port);
	    System.out.println();
	    System.out.println("Listening on port " + serv.getLocalPort() + ". The serverID is " + serverID);
		    
	    s = serv.accept();
	    System.out.println("Server: Connected!");
	}
	catch(Exception e){
	    System.out.println("Failed to connect: " + e);
	    System.exit(1);
	}
	//We need to pass alice
	System.out.println("Enter in password for connection:");

	sysin = new Scanner(System.in);
	password = sysin.next();

	try{
	    G = Utilities.GenG(password);	    
	}
	catch(Exception e){
	    System.out.println("Error in generationg G from password: " + e);
	    System.exit(1);
	}

	try{
	    sIn = s.getInputStream();
	    sOut = s.getOutputStream();

	    while(true){
		if(sIn.available() > 0){
		    readBuffer = new byte[sIn.available()];
		    sIn.read(readBuffer);
		    clientID = new String(readBuffer);
		    sOut.write(1);
		    break;
		}
	    }
	}
	catch(Exception e){
	    System.out.println("Failure retreiving client ID from client: " + e);
	    System.exit(1);
	}

	//System.out.println("DEBUG: Enter in Client ID:");
	//String clientID = sin.next();

	key = DHAuth.Server(G, clientID, serverID, s);
	if(key != null){
	    System.out.println("Client authenticated.");
	    System.out.println("Type your message and press <return> to send. Type '/quit' to close the connection.");
	}
	else{
	    System.out.println("Client did not authenticate. Aborting");
	    System.exit(1);
	}		    
	c = new ChatLib(s, serverID, key);
    }
}
